import serial
import serial.tools.list_ports
from flask import Flask, jsonify, render_template, request

thunder_file = "C:\\Users\\GMZ\\Desktop\\本学期资料\\学校课程\\嵌入式系统\\实验课\\工程\\air-craft-on-ucos-iii\\webUI\\thunder_WebUI.html" # 设置读取的文件路径
thunder = open(thunder_file, "rb")     # 以二进制读取文件内容
thunderUI = thunder.read()
thunder.close()

tower_b_file = "C:\\Users\\GMZ\\Desktop\\本学期资料\\学校课程\\嵌入式系统\\实验课\\工程\\air-craft-on-ucos-iii\\\webUI\\tower_b_WebUI.html" # 设置读取的文件路径
tower_b = open(tower_b_file, "rb")     # 以二进制读取文件内容
tower_b_UI = tower_b.read()
tower_b.close()

tower_r_file = "C:\\Users\\GMZ\\Desktop\\本学期资料\\学校课程\\嵌入式系统\\实验课\\工程\\air-craft-on-ucos-iii\\\webUI\\tower_r_WebUI.html" # 设置读取的文件路径
tower_r = open(tower_r_file, "rb")     # 以二进制读取文件内容
tower_r_UI = tower_r.read()
tower_r.close()

def findPort():     #找到可用串口，否则返回-1
    plist = list(serial.tools.list_ports.comports())
    if len(plist) <= 0:
        print ("The Serial port can't find!")
        return -1
    else:
        plist_0 = list(plist[0])
        serialName = plist_0[0]
        serialFd = serial.Serial(serialName, 9600, timeout = 60)
        print ("check which port was really used =", serialFd.name)
        return serialFd

# 得到Flask 类的实例对象 app
app = Flask(__name__)

@app.route('/')        # 雷电模式web界面
def thunder_():
    global thunderUI
    return thunderUI

@app.route('/', methods=['POST'])
def thunder_control():
    recv = request.json
    print(recv)
    print(type(recv))
    serial = findPort()
    if serial == -1:
        return '-1'
    if recv == 'left':
        serial.write('L'.encode())
    elif recv == 'right':
        serial.write('R'.encode())
    elif recv == 'up':
        serial.write('U'.encode())
    elif recv == 'down':
        serial.write('D'.encode())
    elif recv == 'k1':
        serial.write('1'.encode())
    elif recv == 'k2':
        serial.write('2'.encode())
    elif recv == 'skill':
        serial.write('S'.encode())
    elif recv == 'hero':
        serial.write('H'.encode())
    else:
        serial.write('O'.encode())
    return '1'

@app.route('/blue')    # 塔防模式蓝方web界面
def tower_blue():
    global tower_b_UI
    return tower_b_UI

@app.route('/blue', methods=['POST'])
def blue_control():
    recv = request.json
    print(recv)
    print(type(recv))
    serial = findPort()
    if serial == -1:
        return '-1'
    if recv == 'left':
        serial.write('L'.encode())
    elif recv == 'right':
        serial.write('R'.encode())
    elif recv == 'up':
        serial.write('U'.encode())
    elif recv == 'down':
        serial.write('D'.encode())
    elif recv == 'k1':
        serial.write('1'.encode())
    elif recv == 'k2':
        serial.write('2'.encode())
    elif recv == 'skill':
        serial.write('S'.encode())
    elif recv == 'hero':
        serial.write('H'.encode())
    else:
        serial.write('O'.encode())
    return '1'

@app.route('/red')     # 塔防模式红方web界面
def tower_red():
    global tower_r_UI
    return tower_r_UI

@app.route('/red', methods=['POST'])
def red_control():
    recv = request.json
    print(recv)
    print(type(recv))
    serial = findPort()
    if serial == -1:
        return '-1'
    if recv == 'left':
        serial.write('l'.encode())
    elif recv == 'right':
        serial.write('r'.encode())
    elif recv == 'up':
        serial.write('u'.encode())
    elif recv == 'down':
        serial.write('d'.encode())
    elif recv == 'k1':
        serial.write('3'.encode())
    elif recv == 'k2':
        serial.write('4'.encode())
    elif recv == 'skill':
        serial.write('s'.encode())
    elif recv == 'hero':
        serial.write('h'.encode())
    else:
        serial.write('o'.encode())
    return '1'

if __name__ == '__main__':
    app.run(host='0.0.0.0')